﻿using System;
using System.Collections.Generic;
using System.Text;
using JsonSharp;
using Barbar.MapReport.DataSource;
using JsonSharp.Collections;
using System.Drawing;
using JsonSharp.Values;
using Barbar.MapReport.Common.Drawing;

namespace Barbar.MapReport.HtmlRender.Json {
  internal class PolygonFactory : JsonFactory {
    private Polygon m_Polygon;
    // default google properties
    private Color m_Color;
    private bool m_Fill;
    private bool m_Outline;
    // extended fields
    private Color m_StrokeColor;
    private int m_StrokeWeight;
    private string m_ToolTip;
    private PolygonBehavior m_PolygonBehavior;
    private string m_PolygonName;

    public PolygonFactory(Polygon polygon, Color? fillColor, Color strokeColor, int strokeWeight, bool outline, PolygonBehavior polygonBehavior, string toolTip, string polygonName) {
      if (polygon == null)
        throw new ArgumentNullException("polygon");
      m_Polygon = polygon;
      m_Fill = fillColor.HasValue;
      if (fillColor.HasValue)
        m_Color = fillColor.Value;
      m_StrokeColor = strokeColor;
      m_StrokeWeight = strokeWeight;
      m_Outline = outline;
      m_PolygonBehavior = polygonBehavior;
      m_ToolTip = toolTip;
      m_PolygonName = polygonName;
    }

    public override JsonValue ToJsonObject() {
      JsonObjects result = new JsonObjects();
      JsonArrays polylines = new JsonArrays();
      foreach(Polyline polyline in m_Polygon) {
        PolylineFactory factory = new PolylineFactory(polyline, m_StrokeColor, m_StrokeWeight);
        polylines.Add(factory.ToJsonObject());
      }
      result.Add(new JsonStringValue("polylines"), polylines);
      result.Add(new JsonStringValue("fill"), new JsonBoolValue(m_Fill));
      if (m_Fill) {
        result.Add(new JsonStringValue("color"), new JsonColorValue(m_Color));
        result.Add(new JsonStringValue("opacity"), new JsonNumberValue(((double)m_Color.A) / 255.0));
      }
      result.Add(new JsonStringValue("outline"), new JsonBoolValue(m_Outline));

      JsonObjects behavior = null;
      
      PolygonBehaviorBubble bubbleBehavior = m_PolygonBehavior as PolygonBehaviorBubble;
      if (bubbleBehavior != null) {
        behavior = new JsonObjects();
        behavior.Add(new JsonStringValue("type"), new JsonStringValue(typeof(PolygonBehaviorBubble).Name));
        behavior.Add(new JsonStringValue("description"), new JsonStringValue(bubbleBehavior.GetDescription(m_ToolTip, m_PolygonName)));
      }

      PolygonBehaviorRedirect redirectBehavior = m_PolygonBehavior as PolygonBehaviorRedirect;
      if (redirectBehavior != null) {
        behavior = new JsonObjects();
        behavior.Add(new JsonStringValue("type"), new JsonStringValue(typeof(PolygonBehaviorRedirect).Name));
        behavior.Add(new JsonStringValue("url"), new JsonStringValue(redirectBehavior.Url));
      }

      if (behavior != null)
        result.Add(new JsonStringValue("behavior"), behavior);

      return result;
    }
  }
}
